﻿using FileSC.DB.Database;
using FileSC.DB.Entities;
using FileSC.WEBService.Db;
using Hangfire.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Http;

namespace FileSC.WEBService.Plugins
{
    internal class StatusPlug : PluginBase, IHttpPlugin
    {
        public async Task OnHttpRequest(IHttpSessionClient client, HttpContextEventArgs e)
        {
            var request = e.Context.Request;//http请求体
            var response = e.Context.Response;//http响应体
            if (request.IsPost())
            {
                try
                {
                    switch (request.RelativeURL)
                    {
                        case "/status/download":

                            IList<DownloadJobInfo> down_jobs;
                            using (var db = new DownloadJobDB()) down_jobs = db.GetInfos().OrderByDescending(info => info.CreateTime).ToList();
                            await response
                                    .SetStatus(200, "success")
                                    .FromObj(down_jobs)
                                    .AnswerAsync();
                            return;

                        case "/status/upload":
                            IList<UploadJobInfo> up_jobs;
                            using (var db = new UploadJobDB()) up_jobs = db.GetInfos().OrderByDescending(info => info.CreateTime).ToList();
                            await response
                                    .SetStatus(200, "success")
                                    .FromObj(up_jobs)
                                    .AnswerAsync();
                            return;
                    }
                }
                catch
                {

                }
            }
            await e.InvokeNext();
        }
    }
}
